﻿using KinonekoSoftware.CodariaDB.Documents;
using KinonekoSoftware.CodariaDB.Documents.TemplateContents;

namespace KinonekoSoftware.CodariaDB.Runtimes
{
    public sealed class RepositoryPropertyManager(Repository repository)
    {
        private const string ID_Enable_Ability_LeftTopMaskLayer     = "Ability_Layer_LT";
        private const string ID_Enable_Ability_RightBottomMaskLayer = "Ability_Layer_RB";
        private const string ID_Enable_Ability_ElementIcon          = "Ability_Icon";
        private const string ID_Enable_Ability_TemplatedContentSize = "Ability_Size";
        private const string ID_Enable_Ability_Rarity               = "Ability_Rarity";
        
        

        private const string ID_Property_Name   = "p_name";
        private const string ID_Property_Intro   = "p_intro";
        
        
        private const string ID_IsDisplay_Chikara   = "d_chikara";
        private const string ID_IsDisplay_Elemental = "d_element";
        private const string ID_IsDisplay_Rarity    = "d_rarity";

        private readonly Repository _repository = repository ?? throw new ArgumentNullException(nameof(repository));

        private void UpdateProperty() => _repository.UpdateProperty();


        private IValuePool<string> Strings  => _repository.Strings;
        private IValuePool<int>    Integers => _repository.Integers;
        private IValuePool<bool>   Booleans => _repository.Booleans;

        #region TemplatedContent

        public bool EnableAbilityLeftTopMaskLayer
        {

            get => Booleans[ID_Enable_Ability_LeftTopMaskLayer];
            set
            {
                Booleans[ID_Enable_Ability_LeftTopMaskLayer] = value;
                UpdateProperty();
            }
        }

        public bool EnableAbilityRightBottomMaskLayer
        {

            get => Booleans[ID_Enable_Ability_RightBottomMaskLayer];
            set
            {
                Booleans[ID_Enable_Ability_RightBottomMaskLayer] = value;
                UpdateProperty();
            }
        }

        public bool EnableAbilityElementIcon
        {

            get => Booleans[ID_Enable_Ability_ElementIcon];
            set
            {
                Booleans[ID_Enable_Ability_ElementIcon] = value;
                UpdateProperty();
            }
        }

        public TemplatedContentSize AbilityTemplatedContentSize
        {
            get => (TemplatedContentSize)Integers[ID_Enable_Ability_TemplatedContentSize];
            set
            {
                Integers[ID_Enable_Ability_TemplatedContentSize] = (int)value;
                UpdateProperty();
            }
        }

        public bool IsAbilityRarityHidden
        {
            get => Booleans[ID_Enable_Ability_Rarity];
            set => Booleans[ID_Enable_Ability_Rarity] = value;
        }

        #endregion

        #region SortBy

        public SortBy GetSortBy(InstanceType type)
        {
            var v = Integers[$"SortBy_{type}"];
            return (SortBy)v;
        }

        public SortBy SetSortBy(InstanceType type, SortBy value)
        {
            Integers[$"SortBy_{type}"] = (int)value;
            return value;
        }

        #endregion

        #region PageBy

        public int GetPageBy(InstanceType type)
        {
            return Integers[$"PageBy_{type}"];
        }

        public int SetPageBy(InstanceType type, int value)
        {
            Integers[$"PageBy_{type}"] = value;
            return value;
        }

        #endregion

        #region FilterBy

        public FilterBy GetFilterBy(InstanceType type)
        {
            return (FilterBy)Integers[$"FilterBy_{type}"];
        }

        public FilterBy SetFilterBy(InstanceType type, FilterBy value)
        {
            Integers[$"FilterBy_{type}"] = (int)value;
            return value;
        }

        #endregion

        #region Properties

        public string Name
        {
            get => Strings[ID_Property_Name];
            set => Strings[ID_Property_Name] = value;
        }
        
        public string Intro
        {
            get => Strings[ID_Property_Intro];
            set => Strings[ID_Property_Intro] = value;
        }

        public bool IsDisplayChikaraView
        {
            get => Booleans[ID_IsDisplay_Chikara];
            set => Booleans[ID_IsDisplay_Chikara] = value;
        }

        public bool IsDisplayElementalView
        {
            get => Booleans[ID_IsDisplay_Elemental];
            set => Booleans[ID_IsDisplay_Elemental] = value;
        }

        public bool IsDisplayRarityView
        {
            get => Booleans[ID_IsDisplay_Rarity];
            set => Booleans[ID_IsDisplay_Rarity] = value;
        }
        

        #endregion

        public int DatabaseVersion
        {
            get => Integers[nameof(DatabaseVersion)];
            set
            {
                Integers[nameof(DatabaseVersion)] = value;
            }
        }
    }
}